package com.inspur.gs.sbssdl.dzyhpgl.dzyhplygl.dzyhplygl.front.sbssyhplyqrdetail_frm.voactions;
import com.inspur.edp.bef.api.BefRtBeanUtil;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.parameter.retrieve.RespectiveRetrieveResult;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bff.spi.*;
import com.inspur.edp.bef.api.action.*;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.math.BigDecimal;
import java.util.ArrayList;

public class dypzVOAction extends AbstractFSAction<VoidActionResult> {

	private String id;

	// 申领单ID
	private String slCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhplygl.dzyhplygl.back.sbssDzyhpYhpsl";
	// 库存表ID
	private String kcCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.sbssYhpItem";

	public dypzVOAction(String id) {

		this.id = id;
	}

	/**
	 *  1. 获取申领单中所有的明细项目
	 *  主表中获取归口部门  id , szbh, gkbm
	 *  子表中获取人员、数量、物料、单价 mx -> yhp syry sl dj
	 *
	 *  2. 生成库存表，并预设一个入库单记录
	 *  主表: 使用人员 syryid , 物料 yhpid , 归口部门 gkbmid , 数量 sl , 单价 dj
	 *  新增一个子表数据，标记为入账
	 *  子表: trasType 0  omission 0  djid 主表ID djCode 主表上账编号
	 *
	 *  3.标记申领单 isPz = 1
	 */
	@Override
	public void execute() {
		IStandardLcp slLcp = BefRtBeanUtil.getLcpFactory().createLcp(slCfg);
		IStandardLcp kcLcp = BefRtBeanUtil.getLcpFactory().createLcp(kcCfg);

		IEntityData data = slLcp.retrieve(id).getData();
		if (data == null) {
			return;
		}
		Boolean isPz = (Boolean) data.getValue("isPz");
		if (isPz) return;

		// 上账编号
		String szbh = data.getValue("szbh").toString();
		// 归口部门
		String gkbmId = ((IValuesContainer) data.getValue("gkbm")).getValue("gkbm").toString();

		IEntityDataCollection yhpmx = data.getChilds().get("yhpmx");

		yhpmx.forEach(mx->{
			IEntityData kcData = kcLcp.retrieveDefault();

			ModifyChangeDetail kcDetail = new ModifyChangeDetail(kcData.getID());
			BigDecimal sl = new BigDecimal(mx.getValue("sl").toString());
			BigDecimal dj = new BigDecimal(mx.getValue("dj").toString());

			// 数量 单价
			kcDetail.getPropertyChanges().put("sl", mx.getValue("sl"));
			// sl - sysl = 冻结数量
			kcDetail.getPropertyChanges().put("sysl", mx.getValue("sl"));
			kcDetail.getPropertyChanges().put("dj", dj);
			kcDetail.getPropertyChanges().put("zj",sl.multiply(dj));

			// 使用人员
			AssociationInfo syryInfo = new AssociationInfo();
			syryInfo.setValue("syryid",((IValuesContainer) mx.getValue("syry")).getValue("syry").toString());
			kcDetail.getPropertyChanges().put("syryid", syryInfo);

			// 归口部门
			AssociationInfo gkbmInfo = new AssociationInfo();
			gkbmInfo.setValue("gkbmid",gkbmId);
			kcDetail.getPropertyChanges().put("gkbmid", gkbmInfo);

			// 物料
			AssociationInfo yhpIdInfo = new AssociationInfo();
			yhpIdInfo.setValue("yhpid",((IValuesContainer) mx.getValue("yhp")).getValue("yhp").toString());
			kcDetail.getPropertyChanges().put("yhpid", yhpIdInfo);

			// 首次上账填写上账来源
			AssociationInfo szInfo = new AssociationInfo();
			szInfo.setValue("szdId",id);
			kcDetail.getPropertyChanges().put("szdId",szInfo);

			ArrayList<String> nodeCodes = new ArrayList<>();
			nodeCodes.add("itemtras");
			ArrayList<String> hierarchyIds = new ArrayList<>();
			hierarchyIds.add(kcData.getID());
			IEntityData kcChild = kcLcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

			ModifyChangeDetail mxDetail = new ModifyChangeDetail(kcChild.getID());
			mxDetail.getPropertyChanges().put("trasType","0");
			mxDetail.getPropertyChanges().put("omission","0");
			mxDetail.getPropertyChanges().put("djid",id);
			mxDetail.getPropertyChanges().put("djCode",szbh);

			kcDetail.addChildChangeSet("itemtras",mxDetail);

			kcLcp.modify(kcDetail);
		});

		ModifyChangeDetail djDetail = new ModifyChangeDetail(id);
		djDetail.getPropertyChanges().put("isPz",true);
		slLcp.modify(djDetail);

		slLcp.save();
		kcLcp.save();
	}


}
